<?php
// +----------------------------------------------------------------------
// | 字典管理
// +----------------------------------------------------------------------
namespace app\admin\controller;

use dc\controller\AdminBaseController;
use dragon\Activity\QixiScore;
use dragon\Support\Dictionary;
use dragon\Support\Wechat;
use dragon\User\ThirdPartyUser;
use think\Db;
use think\Validate;

/**
 * 七夕
 */
class QixiController extends AdminBaseController
{
    protected $perpage = 40;

    public function index()
    {
        /**搜索条件**/
        $list = QixiScore::order('highest_score desc')->paginate(40);

        // 获取分页显示
        $page = $list->render();

        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }

    public function notice() {
        $qixiScore = QixiScore::get($this->request->param('user_id', 0, 'intval'));
        $rank = $this->request->param('rank', 0, 'intval');

        // 获取用户信息
        $weUser = ThirdPartyUser::get([
            'user_id'   => $qixiScore['user_id']
        ]);

        $giftCode = mt_rand(10000, 99999);
        $gift = $rank == 1 ? '价值399的装饰画一幅' : '价值38元杯垫一组';
        // 发送通知
        $result = Wechat::sdk()->sendTemplateMessage([
            "touser" => $weUser['openid'],
            "template_id" => "zo3U4ctckcalQqs9yvOqoMGXJOXk4EiYjp355Zcsan8",
            "url" => "http://valentine.12yishu.com/wap/rank.html",
            "topcolor" => "#FF0000",
            "data" => [
                "first" =>  [
                    "value" => $weUser['nickname'].', 恭喜您中奖啦！',
                    "color" => "#173177"	 //参数颜色
                ],
                "keyword1" => [
                    "value" => $giftCode,
                    "color" => "#173177"
                ],
                "keyword2" => [
                    "value" => date('Y-m-d H:i'),
                    "color" => "#173177"
                ],
                "keyword3" => [
                    "value" => $gift,
                    "color" => "#173177"
                ],
                "remark" => [
                    "value" => '恭喜您赢得了我们七夕活动中的奖品，请联系我们的客服 "微信号：xiaoyi--4"进行奖品兑换！',
                    "color" => "#173177"
                ]
            ]
        ]);

        if(!$result) {
            $this->error('发送失败');
        }

        $qixiScore->save([
            'status' => 1,
            'gift'  => $gift,
            'rank'  => $rank,
            'gift_code' => $giftCode,
        ]);

        $this->success('success');
    }
}